<template>
  <div class="shop">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>店铺管理</span>
        <el-button class="saveBtn" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </div>
      <el-form :model="shopForm" label-width="80px">
        <el-form-item label="店铺名称">
          <el-input v-model="shopForm.name"></el-input>
        </el-form-item>
        <el-form-item label="店铺公告">
          <el-input
            type="textarea"
            v-model="shopForm.bulletin"
            rows="5"
          ></el-input>
        </el-form-item>
        <el-form-item label="店铺头像">
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="shopForm.avatar" :src="shopForm.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="店铺图片">
          <el-upload
            :action="action"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="handldSuccess"
            :file-list="shopForm.pics"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item label="配送费">
          <el-input v-model="shopForm.deliveryPrice"></el-input>
        </el-form-item>
        <el-form-item label="配送时间">
          <el-input v-model="shopForm.deliveryTime"></el-input>
        </el-form-item>
        <el-form-item label="配送描述">
          <el-input v-model="shopForm.description"></el-input>
        </el-form-item>
        <el-form-item label="店铺评分">
          <el-rate
            v-model="shopForm.score"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>
        </el-form-item>
        <el-form-item label="销量">
          <span>{{ shopForm.sellCount }}</span>
        </el-form-item>
        <el-form-item label="活动">
          <el-checkbox-group v-model="shopForm.supports">
            <el-checkbox label="在线支付满28减5" name="type"></el-checkbox>
            <el-checkbox label="VC无限橙果汁全场8折" name="type"></el-checkbox>
            <el-checkbox label="单人精彩套餐" name="type"></el-checkbox>
            <el-checkbox label="特价印品8折抢购" name="type"></el-checkbox>
            <el-checkbox label="单人特色套餐" name="type"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="营业时间">
          <el-time-picker
            is-range
            v-model="shopForm.date"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
            value-format="HH:mm:ss"
          >
          </el-time-picker>
        </el-form-item>
        <el-form-item label="起送价格">
          <el-input-number
            v-model="shopForm.minPrice"
            :precision="2"
            :step="0.1"
            :max="100"
          ></el-input-number>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { shopInfo, shopEdit } from "../../api/shop";
export default {
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: "",
      action: "http://127.0.0.1:5000/shop/upload",
      shopForm: {
        name: "",
        bulletin: "",
        avatar: "",
        deliveryPrice: "",
        deliveryTime: "",
        description: "",
        score: 0,
        sellCount: "",
        supports: [],
        date: [],
        minPrice: "",
        pics: [],
      },
    };
  },
  methods: {
    //店铺图片上传成功
    handldSuccess(res) {
      console.log(res);
      let index = res.imgUrl.lastIndexOf("/");
      let name = res.imgUrl.substr(index + 1);
      this.shopForm.pics.push({ name: name, url: res.imgUrl });
      // console.log(this.shopForm.pics);
    },
    //预览图片
    handlePictureCardPreview(file) {
      // console.log(file);
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //删除店铺图片
    handleRemove(file) {
      console.log(file);
      let name = file.name;
      let index = this.shopForm.pics.findIndex((item) => item.name == name);
      //根据name去删除数据
      this.shopForm.pics.splice(index, 1);
    },
    //获取店铺详情
    async loadData() {
      let res = await shopInfo();
      //处理照片墙上的数据
      res.data.pics = res.data.pics.map((item) => {
        let index = item.lastIndexOf("/"); //找最后一个/的位置
        return {
          name: item.substr(index + 1),
          url: item,
        };
      });
      this.shopForm = res.data;
      console.log(this.shopForm);
    },
    //保存修改
    async onSubmit() {
      // let res = await shopEdit(this.shopForm)
      // console.log(this.shopForm)
      let shopObj = JSON.parse(JSON.stringify(this.shopForm));
      let index = shopObj.avatar.lastIndexOf("/");
      shopObj.avatar = shopObj.avatar.substr(index + 1);
      // console.log(shopObj.avatar)
      shopObj.pics = shopObj.pics.map((item) => item.name);
      console.log(shopObj)
      let res = await shopEdit(shopObj)
      let {code,msg} = res;
      if(code === 0){
        this.$message({
          type:"success",
          message:msg,
        })
      }else{
        this.$message.error(msg)
      }
    },
    //头像上传成功
    handleAvatarSuccess(res) {
      console.log(res);
      let { code, msg, imgUrl } = res;
      if (code === 0) {
        this.shopForm.avatar = imgUrl;
      }
    },
    //上传之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    this.loadData();
  },
};
</script>

<style lang="less" scoped>
/deep/.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
/deep/.el-form {
  width: 500px;
}
.el-icon-plus {
  border: 1px solid #ccc;
}
/deep/.el-form-item__content {
  display: flex;
  .avatar-uploader {
    margin-right: 20px;
  }
}
/deep/.el-range-input {
  margin-left: 10px !important;
}
/deep/.el-rate {
  margin-top: 12px;
}
/deep/.el-icon-plus {
  border: none;
}
</style>